package com.admin.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.admin.bean.AdminSpBean;
import com.admin.dao.AdminSpDao;
import com.admin.util.SqlUtil;

@Service("adminSpService")
public class AdminSpService {

	@Autowired private AdminSpDao adminSpDao;
	
	
	
	public int addAdminSp(AdminSpBean spBean){
		//String sql = "insert into adminsp(spname,spcode,spcommand,spprovince,spfee,spmt,spdaylimit,spmonthlimit,spmemo,spstatus,companyid,createdtime) values(?,?,?,?,?,?,?,?,?,?,?,getDate())";
		return adminSpDao.addAdminSp(spBean);
	}
	
	
	public int updateAdminSp(AdminSpBean spBean){
		//String sql = "update adminsp set spname = ?,spcode = ?,spcommand = ?,spprovince = ?,spfee = ?,spmt = ?,spdaylimit = ?,spmonthlimit = ?,spmemo = ?,spstatus = ? where companyid = ?";
		return adminSpDao.updateAdminSp(spBean);
	}
	
	
	public int delAdminSp(int id){
		return adminSpDao.delAdminSp(id);
	}
	
	
	
	
	
	public AdminSpBean findAdminSpById(int id){
		//final String sql = "select * from adminsp where id = ?";
		try {
			return adminSpDao.findAdminSpById(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} finally {

		}
		
	}
	
	public int findCount(){
		//final String sql = "select count(0) from adminsp";
		return adminSpDao.findCount();
	}
	
	
	public int findCount(String condition){
		//final String sql = "select count(0) from adminsp";
		return adminSpDao.findCount(condition);
	}
	
	public List<AdminSpBean> findList(int start,int pageSize){
		try {
			return adminSpDao.findList(start, pageSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ArrayList<AdminSpBean>();
		} finally {

		}
		
	}
	
	public List<AdminSpBean> findList(String condition,int start,int pageSize){
		try {
			return adminSpDao.findList(condition,start, pageSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ArrayList<AdminSpBean>();
		} finally {

		}
		
	}
	
	public int findCountByCompanyId(int companyId){
		//final String sql = "select count(0) from adminsp where compannyid = "+companyId;
		return adminSpDao.findCountByCompanyId(companyId);
	}
	
	public List<AdminSpBean> findListByCompanyId(int start,int pageSize,int companyId){
		//final String sql = SqlUtil.getPageSqlForSqlServerAsc("*", "id", "adminsp", "id", "companyid="+companyId, start, pageSize);//"select * from company order by companyId desc limit ?,?";
		try {
			return adminSpDao.findListByCompanyId(start, pageSize, companyId);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<AdminSpBean>();
		} finally {

		}
		
	}
	
	
	public int findCountBySpCode(String spCode){
		//final String sql = "select count(0) from adminsp where spcode like '%"+spCode+"%'";
		return adminSpDao.findCountBySpCode(spCode);
	}
	
	public List<AdminSpBean> findListBySpCode(int start,int pageSize,String spCode) throws SQLException{
		//final String sql = SqlUtil.getPageSqlForSqlServerAsc("*", "id", "adminsp", "id", "spcode like '%"+spCode+"%'", start, pageSize);//"select * from company order by companyId desc limit ?,?";
		return adminSpDao.findListBySpCode(start, pageSize, spCode);
	}
	
	
	public int findCountBySpName(String spName){
		//final String sql = "select count(0) from adminsp where spname like '%"+spName+"%'";
		return adminSpDao.findCountBySpName(spName);
	}
	
	public List<AdminSpBean> findListBySpName(int start,int pageSize,String spName) throws SQLException{
		//final String sql = SqlUtil.getPageSqlForSqlServerAsc("*", "id", "adminsp", "id", "spcode like '%"+spName+"%'", start, pageSize);//"select * from company order by companyId desc limit ?,?";
		return adminSpDao.findListBySpName(start, pageSize, spName);
	}
}
